package com.eece496.Database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Classroom extends Database{

	private String classroom; 
	private String courseNumber;
	private String sectionNumber;
	
	public Classroom()
	{
		tableName = "Classroom";	
	}
	
	public Classroom(String classroom, String courseNumber, String sectionNumber)
	{
		tableName = "Classroom";
		
		this.classroom = classroom;
		this.courseNumber = courseNumber;
		this.sectionNumber = sectionNumber;
	}
	
	//@requires buildingName !== Null;
    //@assignable newBuildingname,roomNumber;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModBuildingName(String newBuildingName, int tupleID) throws SQLException
	{
		super.Modify(newBuildingName, "buildingname","roomNumber",tupleID); 
	}
	//@requires  className !== Null;
    //@assignable newClassName,roomNumber;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModClassName(String newClassName, int tupleID) throws SQLException
	{
		super.Modify(newClassName, "className","roomNumber",tupleID); 
	}
	
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	@Override
	public void Insert() throws SQLException 
	{
		Statement stmt = connection.createStatement();
				
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" + 
	    					AddQuotes(classroom) + ", " +
	    					AddQuotes(courseNumber) + ", " +
	    					AddQuotes(sectionNumber) + 
	    					")";
	    
	    stmt.executeUpdate(sqlString);
	    System.out.println(sqlString + " complete");
	}
	
	public void DeleteCourseClassroom(String courseNumber, String sectionNumber) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "DELETE from " + tableName + " WHERE courseNumber = " + AddQuotes(courseNumber) +
				" and sectionNumber = " + AddQuotes(sectionNumber);
		stmt.executeUpdate(sqlString);
	}
	
	@Override
    //@assignable stmt,id,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Delete(String primaryKey) throws SQLException 
	{
		Statement stmt = connection.createStatement();
		int id = Integer.parseInt(primaryKey);
		String sqlString = "DELETE from " + tableName + " WHERE roomNumber = " + id;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");
	}
	
	public ArrayList<String> GetListOfClassrooms() throws SQLException
	{
		ArrayList<String> list = new ArrayList<String>();
	    
		String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        list.add(rs.getString("classroom"));
	    }
		
		return list;
	}
	@Override
    //@assignable sqlString, stmt, ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Display() throws SQLException 
	{
	    String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        System.out.println
	        (
	            rs.getString("roomNumber") + " " +
                    rs.getString("buildingName") + " " +
	            rs.getString("classNumber") + " " 
	        );
	    }	
	}
}